@extends('layouts.default') @section('main')
    <script type="text/javascript" src="{{URL::asset('js/')}}/ajaxfileupload.js"></script>
    <script type="text/javascript" src="{{URL::asset('js/')}}/common.js"></script>
    <script src="{{URL::asset('js/My97DatePicker/')}}/WdatePicker.js"></script>

    <div id="content">
        <div id="content-header">
            <div id="breadcrumb">
                <a href="{{url('home')}}" title="返回首页" class="tip-bottom"><i
                            class="icon-home"></i> 首页</a>
                <a href="{{url('game/index')}}" class="tip-bottom">活动-游戏管理</a>
                <a href="#" class="current">编辑一个活动-游戏</a>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span9">
                    <div class="widget-box">
                        <div class="widget-title">
						<span class="icon"> <i class="icon-align-justify"></i>
						</span>
                            <h5>编辑一个活动-游戏</h5>
                        </div>
                        <div class="widget-content nopadding">
                            <form action="{{url('game/edit')}}" method="post" id="adForm" class="form-horizontal">

                                {{--<div class="control-group">--}}
                                    {{--<label class="control-label">活动主题 :</label>--}}
                                    {{--<div class="controls">--}}
                                        {{--<input type="text" class="span11" name="title" placeholder="输入活动主题的一部分，按下Enter键，执行搜索" />--}}
                                    {{--</div>--}}
                                {{--</div>--}}

                                <div class="control-group">
                                    <label class="control-label">活动 :</label>
                                    <div class="controls ">
                                        <select  name="activity_id" class=" span11" >
                                            <option value="{{$activity->id}}" selected >{{$activity->subject}}</option>
                                        </select>
                                    </div>
                                </div>


                                <div class="control-group">
                                    <label class="control-label">游戏类型 :</label>
                                    <div class="controls ">
                                        <select  name="game_type" class=" span11" >
                                            <option value="">请选择</option>
                                            @foreach($types as $k=>$v)
                                                <option value="{{$v->id}}" @if($v->id == $game->type_id) selected @endif>{{$v->name}}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label">游戏名称 :</label>
                                    <div class="controls">
                                        <input type="text" class="span11" name="game_name" placeholder="游戏名称" value="{{$game->title}}" />
                                    </div>
                                </div>

                                @foreach($prizes as $k=>$v)
                                <div class="prize">
                                    <div class="control-group">
                                        <label class="control-label">奖品{{$arr[$k]}} :</label>
                                        <div class="controls">
                                            @if($k>0)
                                            <button type="button" class="del" ref="{{$k+1}}"><i class="icon-trash"></i></button>
                                            @endif
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label">奖品名称 :</label>
                                        <div class="controls">
                                            <input type="text" class="span11" name="prizename{{$k+1}}" placeholder="奖品名称" value="{{$v->title}}"/>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label">奖品类型 :</label>
                                        <div class="controls ">
                                            <select  name="prize_type{{$v+1}}" class=" span11" >
                                                <option value="">请选择</option>
                                                <option value="0" @if(0==$v->type) selected @endif>特等奖</option>
                                                <option value="1" @if(1==$v->type) selected @endif>一等奖</option>
                                                <option value="2" @if(2==$v->type) selected @endif>二等奖</option>
                                                <option value="3" @if(3==$v->type) selected @endif>三等奖</option>
                                                <option value="4" @if(4==$v->type) selected @endif>普通奖</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label">奖品物品 :</label>
                                        <div class="controls ">
                                            <select  name="prize_goods{{$v+1}}" class="prize_goods span11" >
                                                <option value="">请选择</option>
                                                @foreach($goods as $k=>$val)
                                                    <option value="{{$val->id}}" @if($v->id==$v->goods_id) selected @endif>{{$val->name}}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label">奖品数量 :</label>
                                        <div class="controls">
                                            <input type="text" class="span11 num" name="number{{$v+1}}" placeholder="奖品数量" style="width:100px" value="{{$v->num}}" /><label class="stock"></label>
                                        </div>
                                    </div>
                                </div>
                                @endforeach

                                <div class="control-group" id="addPrize">
                                    <label class="control-label">继续添加奖品 :</label>
                                    <div class="controls">
                                        <button id="add" type="button"><i class="icon-plus"></i></button>
                                    </div>
                                </div>



                                <input type="hidden" class="span11" name="formType" value="create" />
                                <div class="form-actions">
                                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                                    <button type="button" id="doSave" class="btn btn-success">创建</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="{{URL::asset('js/')}}/common.js"></script>

    <script>

        var n = {{count($prizes)}};
        var myNum=new Array('零','一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五');
        $("#add").click(function(){
            if(n>14){
                alert('奖项也太多了吧!');
                return ;
            }
            n++;
            var html='';
            html+='<div class="prize">';
            html+='<div class="control-group">';
            html+='<label class="control-label" class="prizeText">奖品'+myNum[n]+' :</label>';
            html+='<div class="controls">';
            html+='<button type="button" class="del" ref="'+n+'"><i class="icon-trash"></i></button>';
            html+='</div>';
            html+='</div>';

            html+='<div class="control-group">';
            html+='<label class="control-label">奖品名称 :</label>';
            html+='<div class="controls">';
            html+='<input type="text" class="span11" name="prizename'+n+'" placeholder="奖品名称"/>';
            html+='</div>';
            html+='</div>';

            html+='<div class="control-group">';
            html+='<label class="control-label">奖品类型 :</label>';
            html+='<div class="controls ">';
            html+='<select  name="prize_type'+n+'" class="type span11" >';
            html+='<option value="">请选择</option>';
            html+='<option value="0">特等奖</option>';
            html+='<option value="1">一等奖</option>';
            html+='<option value="2">二等奖</option>';
            html+='<option value="3">三等奖</option>';
            html+='<option value="4">普通奖</option>';
            html+='</select>';
            html+='</div>';
            html+='</div>';

            html+='<div class="control-group">';
            html+='<label class="control-label">奖品物品 :</label>';
            html+='<div class="controls ">';
            html+='<select  name="prize_goods'+n+'" class="prize_goods span11" >';
            html+='<option value="">请选择</option>';
            html+='@foreach($goods as $k=>$v)';
            html+='<option value="{{$v->id}}">{{$v->name}}</option>';
            html+='@endforeach';
            html+='</select>';
            html+='</div>';
            html+='</div>';

            html+='<div class="control-group">';
            html+='<label class="control-label">奖品数量 :</label>';
            html+='<div class="controls">';
            html+='<input type="text" class="span11 num" name="number'+n+'" placeholder="奖品数量" style="width:100px" /><label class="stock"></label>';
            html+='</div>';
            html+='</div>';
            html+='</div>';

            $("#addPrize").before(html);
        });


        $(document).on('click','.del',function(){
            $(this).parents(".prize").remove();
            var nownum =  $(this).attr('ref');
            $(".del").each(function(){
                if($(this).attr('ref')>nownum) {
                    $(this).parent().prev().text('奖品'+myNum[$(this).attr('ref')-1]);
                    $(this).attr('ref',$(this).attr('ref')-1);
                }
            });
            n--;
        });


        $(document).on('change','.prize_goods',function(){
            param = {};
            param['id'] = $(this).val();
            var _this = $(this).parents(".prize").find(".stock");
            var nowNum = $(this).parents(".prize").find(".num").val();

            ajaxRequest(param, '/game/stock', function(data){
                if(data.status){
                    if(parseInt(nowNum)>parseInt(data.message.stock)){
                        alert('你所填写的奖品数量超出了该项奖品的库存');
                    }
                    _this.text('库存：'+data.message.stock);
                }else{
                    _this.text('库存：'+data.message);
                }
            });
        });

        $(document).on('blur','.num',function(){
            var id = $(this).parents(".prize").find(".prize_goods").val();
            if(isNaN(id)){
                alert('请先选择奖品物品');
            }
            param = {};
            param['id'] = id;
            var nowNum = $(this).val();

            ajaxRequest(param, '/game/stock', function(data){
                if(data.status){
                    if(parseInt(data.message.stock)<parseInt(nowNum)){
                        alert('你所填写的奖品数量超出了该项奖品的库存');
                        return false;
                    }
                }else{
                    _this.text('请先选择奖品物品');
                }
            });

        });


        $("#doSave").click(function(){
            $(this).attr('disabled','disabled');

            ajaxRequest($('#adForm').serializeArray(),$('#adForm').attr('action'),function(data){
                if(data.status){
                    alert(data.message);
                    window.location.href = data.forwardUrl;
                }else{
                    alert(data.message);
                }
            });
        });
    </script>
@stop
